/*
 * Created on Mar 22, 2006
 * Modified on Mar 27, 2006
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */

package edu.byu.isys413.group1E.handlers;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**This is the class for the MyDate object to facilitate date manipulation and comparison.
 * @author Travis Kulbeth
 *
 */
public class MyDate {
    private int month;
    private int day;
    private int year;
    
    /**
     * Constructor for MyDate
     */
    public MyDate(){
    }
    
    /** Constructor for MyDate
     *@param m contains the month
     *@param d contains the day
     *@param y contains the year
     */
    public MyDate(int m, int d, int y){
        month = m;
        day = d;
        year = y;
    }
    
    /** Returns the month
     *@return month
     */
    public int getMonth(){return month;	}
    
    /** Returns the day
     *@return day
     */
    public int getDay(){return day;	}
    
    /** Returns the Year
     *@return year
     */
    public int getYear(){return year;}
    
    /** Returns the date in a String format YYYY-MM-DD
     *@return YYYY-MM-DD
     */
    public String getFullDateString(){
        return new String(Integer.toString(year) + "-" + Integer.toString(month) + "-" + Integer.toString(day));
    }
    
    /** This method receives a string date and converts it to a MyDate object.
     *@param date contains the date in string format "YYYY-MM-DD"
     *@return MyDate object
     */
    public MyDate stringToMyDate(String date){
        //parse the month, day, and year from the string
        try {//get the month
            this.month = Integer.parseInt(date.substring(5, 7));
            try {//if the month is two digits get the day at the following location
                this.day = Integer.parseInt(date.substring(8,10));
            }catch (Exception ex) { //if the day is only one digit, get it at the following location
                this.day = Integer.parseInt(date.substring(8,9));
            }
        } catch (NumberFormatException ex) {    //if the month is only one digit
            this.month = Integer.parseInt(date.substring(5,6));
            try {//if the month is one digit get the day at the following location
                this.day = Integer.parseInt(date.substring(7,9));
            }catch (Exception ex2) { //if the day is only one digit, get it at the following location
                this.day = Integer.parseInt(date.substring(7,8));
            }
        }//get the year
        this.year = Integer.parseInt(date.substring(0,4));
        return new MyDate(this.month, this.day, this.year);
    }
    
    /**
     * Compares two MyDate objects to determine if they are equal
     * @param z contains a MyDate object
     * @return a boolean indicating whether the MyDate objects are equal
     */
    public boolean equals(MyDate z){
        if(year == z.year){
            if(month == z.month){
                if(day == z.day){
                    return true;
                }
            }
        }
        return false;	//if one of the criteria does not go through, return false
    }
    
    /** Adds a day to the MyDate object.  This method accounts for leap years and differences in
     *the length of months.
     */
    public void addDay(){
        //use array to compare days
        int days[] = {31,isLeapYear(),31,30,31,30,31,31,30,31,30,31};
        //increment day by 1
        day = day + 1;
        //check if still valid
        if(day > days[month-1]){
            month++;
            day = 1;	//set to beginning of following month
            if(month > 12){
                year++;
                month = 1;
            }
        }
    }
    
    /** This method adds a specified number of days to the MyDate object.
     *@param n contains the number of days to be added.
     */
    public void addDay(int n){
        //increment day by 1
        while(n > 0){
            addDay();
            n--;
        }//end while
    }//end method addDay
    
    /** This method is to determine if the MyDate year is a leap year and returns the
     *number of days in February.
     *
     *@return number of days in this year's february.
     */
    private int isLeapYear(){
        int febDays = 28;
        if(((year%4 == 0) && (year%100 != 0))|| (year%400 == 0)){
            febDays = 29;
        }
        return febDays;
    }
    
    /** Prints out the Date in the format mm/dd/yyyy
     *@return date in format mm/dd/yyyy
     */
    public String printDate(){
        String date;
        date = Integer.toString(month);
        date = date.concat("/");
        date = date.concat(Integer.toString(day));
        date = date.concat("/");
        date = date.concat(Integer.toString(year));
        return date;
    }
    
    /** Shows the entire date as a string such as 'January 1, 1999'
     *@return string like 'January 1, 1999'
     */
    public String showDate(){
        String wholeDate;
        String monthStr[] = {"January","February","March","April","May","June","July","August","September",
        "October","November","December"};
        
        //build finished string
        wholeDate = monthStr[month-1];	//get name of month from array (one less than the month number)
        wholeDate = wholeDate.concat(" ");
        wholeDate = wholeDate.concat(Integer.toString(day));
        wholeDate = wholeDate.concat(", ");
        wholeDate = wholeDate.concat(Integer.toString(year));
        
        return wholeDate;
    }
    
    /** Compares two dates to determine if the indicated date is less than
     *the current MyDate.
     *@param md contains the indicated date.
     *@return whether the indicated date is less than the current date.
     */
    public boolean isLessThan(MyDate md){
        boolean tf = true;
        if(year > md.year){
            tf = false;
        }else if(year == md.year){
            if(month > md.month){
                tf = false;
            }else if(month == md.month){
                if(day >= md.day){
                    tf = false;
                }
            }
        }
        return tf;
    }
    
    /** Compares two dates to determine if the indicated date is greater than
     *the current MyDate.
     *@param md contains the indicated date.
     *@return whether the indicated date is greater than the current date.
     */
    public boolean isGreaterThan(MyDate md){
        boolean tf = true;
        if(isLessThan(md)){
            tf = false;
        }else if(equals(md)){
            tf = false;
        }
        return tf;
    }
    public int daysBetween(String date1, String date2) {
        MyDate md1 = stringToMyDate(date1);
        MyDate md2 = stringToMyDate(date2);
        int daysBetween = 0;
        
        if (md1.isGreaterThan(md2)) {
            while (md2.isLessThan(md1)) {
                md2.addDay();
                daysBetween++;
            }
            return daysBetween;
        }else
            return 0;
    }
    
    /**gets today's date in SimpleDateFormat and returns it as a string*/
    public String getTodaysDate(){
        Calendar cal = Calendar.getInstance();
        Date today = cal.getTime();
        SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
        String todaysDate = fmt.format(today);
        return todaysDate;
    }
}
